/*
 ------------------------------------------------------------------------------
 Copyright (C) 2006-2007 Team Blur.

 This file is part of the Quantum Engine source code.

 The Quantum Engine source code is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at your
 option) any later version.

 The Quantum Engine source code is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 more details.

 You should have received a copy of the GNU General Public License along with
 the Quantum Engine source code; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ------------------------------------------------------------------------------
*/


#ifndef __STRING_H__
#define __STRING_H__


// Color escape strings
#define S_COLOR_DEFAULT			"^b"

#define S_COLOR_BLACK			"^0"
#define S_COLOR_RED				"^1"
#define	S_COLOR_GREEN			"^2"
#define S_COLOR_YELLOW			"^3"
#define S_COLOR_BLUE			"^4"
#define S_COLOR_CYAN			"^5"
#define S_COLOR_MAGENTA			"^6"
#define S_COLOR_WHITE			"^7"

// Color escape characters
#define C_COLOR_DEFAULT			'b'

#define C_COLOR_BLACK			'0'
#define C_COLOR_RED				'1'
#define	C_COLOR_GREEN			'2'
#define C_COLOR_YELLOW			'3'
#define C_COLOR_BLUE			'4'
#define C_COLOR_CYAN			'5'
#define C_COLOR_MAGENTA			'6'
#define C_COLOR_WHITE			'7'

// Colored string macros
#define Str_IsColor(s)			((s) && *(s) == '^' && ((s)[1] && (s)[1] != '^'))
#define Str_GetColor(c, color)	(color = colorTable[((c) - '0') & COLOR_MASK])
#define Str_GetColorIndex(c)	(((c) - '0') & COLOR_MASK)


INLINE bool Str_CharIsAlpha (int c){

	if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z'))
		return false;

	return true;
}

INLINE bool Str_CharIsDigit (int c){

	if (c < '0' || c > '9')
		return false;

	return true;
}

INLINE bool Str_CharIsPunctuation (int c){

	if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
		return false;

	if (c < 32 || c > 127)
		return false;

	return true;
}

INLINE bool Str_CharIsPrintable (int c){

	if (c < 32 || c > 127)
		return false;

	return true;
}

INLINE bool Str_CharIsSpace (int c){

	if (c != '\f' && c != '\n' && c != '\r' && c != '\t' && c != '\v' && c != ' ')
		return false;

	return true;
}

INLINE bool Str_CharIsLower (int c){

	if (c < 'a' || c > 'z')
		return false;

	return true;
}

INLINE bool Str_CharIsUpper (int c){

	if (c < 'A' || c > 'Z')
		return false;

	return true;
}

INLINE int Str_CharToLower (int c){

	if (c >= 'A' && c <= 'Z')
		c += 'a' - 'A';

	return c;
}

INLINE int Str_CharToUpper (int c){

	if (c >= 'a' && c <= 'z')
		c += 'A' - 'a';

	return c;
}


// String manipulation functions
int				Str_Length (const char *string);
int				Str_LengthWithoutColors (const char *string);
void			Str_RemoveColors (char *string);
void			Str_Copy (char *dst, const char *src, int dstSize);
void			Str_Append (char *dst, const char *src, int dstSize);
int				Str_Compare (const char *string1, const char *string2);
int				Str_CompareChars (const char *string1, const char *string2, int n);
int				Str_ICompare (const char *string1, const char *string2);
int				Str_ICompareChars (const char *string1, const char *string2, int n);
void			Str_ToLower (char *string);
void			Str_ToUpper (char *string);
float			Str_ToFloat (const char *string);
int				Str_ToInteger (const char *string);
const char *	Str_FromFloat (float value);
const char *	Str_FromInteger (int value);
const char *	Str_FromFloatArray (const float *values, int elements, int precision);
const char *	Str_FromIntegerArray (const int *values, int elements);
char *			Str_FindChar (const char *string, int c);
char *			Str_FindText (const char *string, const char *text, bool caseSensitive);
int				Str_SPrintf (char *dst, int dstSize, const char *fmt, ...);
int				Str_VSPrintf (char *dst, int dstSize, const char *fmt, va_list argPtr);
const char *	Str_VarArgs (const char *fmt, ...);

// Matches the given filter against the given string
bool			Str_MatchFilter (const char *string, const char *filter, bool caseSensitive);

// Returns a hash key for the given string.
// The hashSize parameter must be a power of two value.
uint			Str_HashKey (const char *string, uint hashSize, bool caseSensitive);

// Directory / file name manipulation functions
void			Str_StripFilePath (char *path);
void			Str_StripFileExtension (char *path);
void			Str_DefaultFilePath (char *path, int pathSize, const char *defaultPath);
void			Str_DefaultFileExtension (char *path, int pathSize, const char *defaultExtension);
void			Str_ExtractFilePath (const char *src, char *dst, int dstSize);
void			Str_ExtractFileName (const char *src, char *dst, int dstSize);
void			Str_ExtractFileBase (const char *src, char *dst, int dstSize);
void			Str_ExtractFileExtension (const char *src, char *dst, int dstSize);


#endif	// __STRING_H__
